#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import argparse

parser = argparse.ArgumentParser()
parser.add_argument(
    '-f'
    ,type=str
    ,required=True
    ,help='.rsf文件的路径'
)
parser.add_argument(
    '-s'
    ,type=bool
    ,default=True
    ,help='是否去除自环边'
)
parser.add_argument(
    '-g'
    ,type=str
    ,default=None
    ,help='如果指定ground truth文件的路径，则计算依赖图与ground truth的节点覆盖率'
)
args=parser.parse_args()
 
with open(args.f,'r') as f:
    nodes=set()
    edges=set()
    self_loop_cnt = 0
    lines=f.readlines()
    for line in lines:
        tokens=line.split()
        nodes.add(tokens[1])
        nodes.add(tokens[2])
        if tokens[1] == tokens[2] and args.s == True:
            self_loop_cnt = self_loop_cnt + 1
            continue
        else:
            edges.add((tokens[1],tokens[2]))
    print("nodes number: ",len(nodes))
    print("edges number: ",len(edges))
    print("detect self loop: ",self_loop_cnt)

if args.g != None:
    with open(args.g,'r') as f:
        lines = f.readlines()
        gt_nodes = set()
        for line in lines:
            tokens = line.split()
            node = tokens[2]
            gt_nodes.add(node)
    # rsf_gt = nodes.difference(gt_nodes)
    # gt_rsf = gt_nodes.difference(nodes)
    # nodes & gt_nodes
    rsf_gt = nodes-gt_nodes
    gt_rsf = gt_nodes-nodes
    # nodes.intersection(gt_nodes)
    coverage_nodes = nodes & gt_nodes
    print(f"ground truth nodes number: {len(gt_nodes)}")
    print(f"coverage of nodes between rsf and ground truth: {len(coverage_nodes)/len(gt_nodes)}")
    print(f"{len(rsf_gt)} nodes in rsf but not in ground truth:")
    for node in rsf_gt:
        print("\t",node)
    print(f"{len(gt_rsf)} nodes in ground truth but not in rsf:")
    for node in gt_rsf:
        print("\t",node)